package cn.bdqn.controller.finance;

import java.beans.IntrospectionException;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.mysql.jdbc.StringUtils;

import cn.bdqn.pojo.Depatement;
import cn.bdqn.pojo.Finance;
import cn.bdqn.pojo.Job;
import cn.bdqn.service.finance.FinanceService;
import cn.bdqn.service.finance.SalaryService;
import cn.bdqn.tools.Constants;
import cn.bdqn.tools.PageSupport;

@Controller
@RequestMapping("/finance")
public class FinanceController {
	@Resource
	private FinanceService fs;
	@Resource
	private SalaryService salaryService;

	@RequestMapping("/import")
	public String importFin(HttpServletRequest request, Model model) throws Exception {  
	     int adminId = 1;  
	     //获取上传的文件  
	     MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;  
	     MultipartFile file = multipart.getFile("upfile");  
	     String month = request.getParameter("month");  
	     InputStream in = file.getInputStream();  
	     //数据导入  
	     salaryService.importExcelInfo(in,file,month,adminId);  
	     in.close();  
	     return "error";  
	}
	@RequestMapping("/export")
	public void expor(HttpServletRequest request, HttpServletResponse response) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException{
		String fileName = request.getParameter("filename");  
		System.out.println("sssss"+fileName);
	    if(fileName!=""){  
	        response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        // 指定下载的文件名  
	        response.setHeader("Content-Disposition", "attachment;filename="+fileName+".xlsx");  
	        response.setContentType("application/vnd.ms-excel;charset=UTF-8");  
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);  
	        XSSFWorkbook workbook=null;  
	        //导出Excel对象  
	        workbook = salaryService.exportExcelInfo();  
	        OutputStream output;  
	        try {  
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush();  
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
	@RequestMapping("/delfinance")
	public String delfinance(@RequestParam("finid") String finid) {
		int i = fs.delFinance(Integer.parseInt(finid));
		if (i > 0) {
			return "redirect:/finance/list";
		} else {
			return "error";
		}
	}

	@RequestMapping("/list")
	public String list(
			@RequestParam(value = "queryEmpName", required = false) String empName,
			@RequestParam(value = "queryYear", required = false) String year,
			@RequestParam(value = "queryMonth", required = false) String month,
			@RequestParam(value = "queryDepId", required = false) String depId,
			@RequestParam(value = "pageIndex", required = false) String pageIndex,
			Model m) {
		if (StringUtils.isNullOrEmpty(empName)) {
			empName = "";
		}
		if (StringUtils.isNullOrEmpty(year)) {
			year = "";
		}
		if (StringUtils.isNullOrEmpty(month)) {
			month = "";
		}
		if (StringUtils.isNullOrEmpty(depId)) {
			depId = "0";
		}
		if (StringUtils.isNullOrEmpty(pageIndex)) {
			pageIndex = "1";
		}
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(Integer.parseInt(pageIndex));
		pages.setPageSize(Constants.pageSize);
		int count = fs.getFinanceCount(empName,Integer.parseInt(depId),year,month);
		pages.setTotalCount(count);
		int from = pages.getPageSize() * (pages.getCurrentPageNo() - 1);
		List<Finance> list = null;
		List<Job> list1 = null;
		List<Depatement> list2 = null;
		list = fs.getlist(empName,
				Integer.parseInt(depId),year,month, from, pages.getPageSize());
		System.out.println(">>>>>>list" + list);
		list2 = fs.getDepList();
		m.addAttribute("financelist", list);
		m.addAttribute("joblist", list1);
		m.addAttribute("deplist", list2);
		if (empName != null) {
			m.addAttribute("empName", empName);
		}
		if (year != null) {
			m.addAttribute("year", year);
		}
		if (month != null) {
			m.addAttribute("month", month);
		}
		System.out.println(">>>>>>list" + list1 + ">>>>>>list2" + list2);
		m.addAttribute("pages", pages);
		return "financelist";
	}

}
